#include <cqcppsdk/cqcppsdk.h>
#include <hdustea.hpp>

#define CMD_SYMBOL '#'

mongocxx::uri mongoDBUri("mongodb://localhost:27017");

CQ_INIT {
    on_enable([] {
        logging::info("启用", "插件已启用");
    });

    on_private_message([](const PrivateMessageEvent &e) {

        /*try {
            auto msgid = send_private_message(e.user_id, e.message); // 直接复读消息
            logging::info_success("私聊", "私聊消息复读完成, 消息 Id: " + to_string(msgid));
            send_message(e.target,
                         MessageSegment::face(111) + "这是通过 message 模块构造的消息~"); // 使用 message 模块构造消息
        } catch (ApiError &e) {
            logging::warning("私聊", "私聊消息复读失败, 错误码: " + to_string(e.code));
        }*/

    });

    on_message([](const MessageEvent &e) {
        //logging::debug("消息", "收到消息: " + e.message + "\n实际类型: " + typeid(e).name());
    });

    on_group_message([](const GroupMessageEvent &event) {
        if (event.message[0] == CMD_SYMBOL) {
            mongocxx::client mongoClient(mongoDBUri);
            mongocxx::database privilegeDatabase = mongoClient.database("PrivilegeDB");
            int32_t userAuthLevel = hdustea::group::GetUserAuthLevel(to_string(event.group_id), event.user_id, privilegeDatabase);
            stringstream commandStream(event.message);
            vector<string> paramList;
            while (!commandStream.eof()) {
                smatch submatch;
                string tempParam;
                commandStream >> tempParam;
                if (regex_match(tempParam, submatch, regex(R"((\s*))"))) {
                    logging::debug("CommandList", tempParam);
                } else {
                    paramList.push_back(tempParam);
                }
            }
            switch (hdustea::group::CommandCheck(paramList[0], GROUP_AVAILABLE_COMMANDS)) {
                case 0 :    // #h
                    hdustea::group::manage::CommandHelp(event.group_id, paramList.size());
                    break;
                case 1 :    // #ban
                    hdustea::group::manage::CommandBan(event.group_id, event.user_id, paramList, userAuthLevel, privilegeDatabase);
                    break;
                case 2 :    // #unban
                    hdustea::group::manage::CommandUnban(event.group_id, event.user_id, paramList, userAuthLevel, privilegeDatabase);
                    break;
                default:
                    logging::debug("Command", "Command not found.");
                    break;
            }
        }

        /*static const set<int64_t> ENABLED_GROUPS = {123456, 123457};
        if (ENABLED_GROUPS.count(event.group_id) == 0) return; // 不在启用的群中, 忽略

        try {
            send_message(event.target, event.message); // 复读
            auto mem_list = get_group_member_list(event.group_id); // 获取群成员列表
            string msg;
            for (auto i = 0; i < min(10, static_cast<int>(mem_list.size())); i++) {
                msg += "昵称: " + mem_list[i].nickname + "\n"; // 拼接前十个成员的昵称
            }
            send_group_message(event.group_id, msg); // 发送群消息
        } catch (ApiError &) { // 忽略发送失败
        }
        if (event.is_anonymous()) {
            logging::info("群聊", "消息是匿名消息, 匿名昵称: " + event.anonymous.name);
        }
        event.block(); // 阻止当前事件传递到下一个插件*/
    });

    on_group_upload([](const auto &e) { // 可以使用 auto 自动推断类型
        stringstream ss;
        ss << "您上传了一个文件, 文件名: " << e.file.name << ", 大小(字节): " << e.file.size;
        try {
            send_message(e.target, ss.str());
        } catch (ApiError &) {
        }
    });
}

CQ_MENU(menu_demo_1) {
    logging::info("菜单", "点击菜单1");
}

CQ_MENU(menu_demo_2) {
    send_private_message(10000, "测试");
}
